<?php
require_once 'phpmoko/lib/common.php';

require_once 'phpmoko/ui/HTML.php';
require_once 'phpmoko/ui/Widget.php';

class FlexTable extends Widget
{
	private $spacing = 0;
	private $widgets = array();
	private $widgetsIds = array();

	function __construct()
	{
		parent::__construct();
	}

	public function setSpacing($spacing)
	{
		$this->spacing = $spacing;
	}

	/**
	 *
	 * @param int $x
	 * @param int $y
	 * @param Widget $widget
	 */
	public function setWidget($x, $y, $widget)
	{
		if ($widget instanceof Widget)
			$this->widgets[] = $widget;
		else
			$this->widgets[] = new HTML($widget);

		$this->widgetsIds[$x][$y] = count($this->widgets) - 1;
	}

	/**
	 *
	 * @param int $x
	 * @param int $y
	 * @return Widget:
	 */
	public function getWidget($x, $y)
	{
		return $this->widgets[$this->widgetsIds[$x][$y]];
	}

	public function setColSpan($x, $y, $cols)
	{
		for ($i = $x; $i < ($x + $cols); $i++)
			$this->widgetsIds[$i][$y] = $this->widgetsIds[$x][$y];
	}

	public function setCellHorizontalAlignment($x, $y, $alignment)
	{
		$this->widgets[$this->widgetsIds[$x][$y]]->setHorizontalAlignment($alignment);
	}

	public function setCellVerticalAlignment($x, $y, $alignment)
	{
		$this->widgets[$this->widgetsIds[$x][$y]]->setVerticalAlignment($alignment);
	}

	function toString()
	{
		$widthMax = 0;
		$heightMax = 0;
		$width = 0;
		foreach ($this->widgetsIds as $key => $value) {
			$height = 0;
			foreach ($this->widgetsIds[$key] as $key2 => $value2)
				$height++;

			if ($heightMax < $height)
				$heightMax = $height;

			$width++;
		}

		if ($widthMax < $width)
			$widthMax = $width;

		$width = $widthMax;
		$height = $heightMax;

		// Draw the table generated
		$str = '<table cellpadding="0" cellspacing="' . $this->spacing . '"';

		$str .= ' id="' . $this->id . '"';

		if ($this->getHorizontalAlignment() != null)
			$str .= ' align="' . $this->getHorizontalAlignment() . '"';

		if ($this->style != null)
			$str .= ' class="' . $this->style . '"';

		if ($this->getWidth() != null)
			$str .= ' width="' . $this->getWidth() . '"';

		$str .= '>';

		$draws = array();

		for ($i = 0; $i < $height; $i++) {
			$str .= '<tr class="row'.$i.'">';
			for ($j = 0; $j < $width; $j++) {
				$widgetPos = null;

				if (isset($this->widgetsIds[$j][$i]))
					$widgetPos = $this->widgetsIds[$j][$i];

				if ((array_key_exists($widgetPos, $draws)) && ($draws[$widgetPos] == -1))
					continue;

				$widgetCols = 0;
				$widgetRows = 0;
				for ($k = 0; $k < $width; $k++)
					if (isset($this->widgetsIds[$k][$i]) && ($this->widgetsIds[$k][$i] == $widgetPos))
						$widgetCols++;

				for ($k = 0; $k < $height; $k++)
					if (isset($this->widgetsIds[$j][$k]) && ($this->widgetsIds[$j][$k] == $widgetPos))
						$widgetRows++;

				$str .= '<td class="col'.$j.' row'.$j.'x'.$i.'"';
				if ($widgetRows > 1)
					$str .= ' rowspan="' . $widgetRows . '"';

				if ($widgetCols > 1)
					$str .= ' colspan="' . $widgetCols . '"';

				if (($widgetPos !== null) && ($this->widgets[$widgetPos] != null)) {
					if ($this->widgets[$widgetPos]->getWidth() != null)
						$str .= ' width="' . $this->widgets[$widgetPos]->getWidth() . '"';

					if ($this->widgets[$widgetPos]->getHorizontalAlignment() != null)
						$str .= ' align="' . $this->widgets[$widgetPos]->getHorizontalAlignment() . '"';

					if ($this->widgets[$widgetPos]->getVerticalAlignment() != null)
						$str .= ' valign="' . $this->widgets[$widgetPos]->getVerticalAlignment() . '"';
				}

				$str .= '>';

				if (($widgetPos !== null) && ($this->widgets[$widgetPos] != null))
					$str .= Widget::toStringSafe($this->widgets[$widgetPos]);
				else
					$str .= '&nbsp;';

				$str .= '</td>';

				$draws[$widgetPos] = -1;
			}

			$str .= '</tr>';
		}

		$str .= '</table>';

		return $str;
	}
}
?>